package com.ruoyi.project.stat.paeaReportBusiness.service.impl;

import java.sql.Date;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.security.ShiroUtils;
import com.ruoyi.project.stat.paeaReportBusiness.domain.StatisticsReportBusiness;
import com.ruoyi.project.system.dept.domain.Dept;
import com.ruoyi.project.system.dept.mapper.DeptMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.stat.paeaReportBusiness.mapper.PaeaReportBusinessMapper;
import com.ruoyi.project.stat.paeaReportBusiness.domain.PaeaReportBusiness;
import com.ruoyi.project.stat.paeaReportBusiness.service.IPaeaReportBusinessService;
import com.ruoyi.common.utils.text.Convert;
import org.springframework.ui.ModelMap;

/**
 * 差错处理统计Service业务层处理
 *
 * @author gang.wei
 * @date 2020-04-14
 */
@Service
public class PaeaReportBusinessServiceImpl extends ServiceImpl<PaeaReportBusinessMapper, PaeaReportBusiness> implements IPaeaReportBusinessService
{
    @Autowired
    private PaeaReportBusinessMapper paeaReportBusinessMapper;
    @Autowired
    private DeptMapper deptMapper;
    /**
     * 查询差错处理统计
     *
     * @param id 差错处理统计ID
     * @return 差错处理统计
     */
    @Override
    public PaeaReportBusiness selectPaeaReportBusinessById(String id)
    {
        return paeaReportBusinessMapper.selectPaeaReportBusinessById(id);
    }

    /**
     * 查询差错处理统计列表
     *
     * @param paeaReportBusiness 差错处理统计
     * @return 差错处理统计
     */
    @Override
    public List<PaeaReportBusiness> selectPaeaReportBusinessList(PaeaReportBusiness paeaReportBusiness)
    {
        return paeaReportBusinessMapper.selectPaeaReportBusinessList(paeaReportBusiness);
    }

    /**
     * 新增差错处理统计
     *
     * @param paeaReportBusiness 差错处理统计
     * @return 结果
     */
    @Override
    public int insertPaeaReportBusiness(PaeaReportBusiness paeaReportBusiness)
    {
        return paeaReportBusinessMapper.insertPaeaReportBusiness(paeaReportBusiness);
    }

    /**
     * 修改差错处理统计
     *
     * @param paeaReportBusiness 差错处理统计
     * @return 结果
     */
    @Override
    public int updatePaeaReportBusiness(PaeaReportBusiness paeaReportBusiness)
    {
        return paeaReportBusinessMapper.updatePaeaReportBusiness(paeaReportBusiness);
    }

    /**
     * 删除差错处理统计对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deletePaeaReportBusinessByIds(String ids)
    {
        return paeaReportBusinessMapper.deletePaeaReportBusinessByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除差错处理统计信息
     *
     * @param id 差错处理统计ID
     * @return 结果
     */
    @Override
    public int deletePaeaReportBusinessById(String id)
    {
        return paeaReportBusinessMapper.deletePaeaReportBusinessById(id);
    }

    @Override
    public void init(ModelMap mmap) {
        List<Dept> depts = deptMapper.selectAllDepts();
        Map deptMap = new HashMap();
        for(Dept dept:depts){
            deptMap.put(dept.getDeptId(),dept.getDeptName());
        }
        mmap.put("deptMap",deptMap);
        mmap.put("defaultOrg",ShiroUtils.getSysUser().getDeptId());
    }

    @Override
    public List<StatisticsReportBusiness> queryBusiness(Date beginOperateDate, Date endOperateDate, String orgCode, int statType, String userRoles) {
        List<StatisticsReportBusiness> result = new ArrayList<StatisticsReportBusiness>();
        Map<String,Object> quereParams = new HashMap<String,Object>();
        quereParams.put("beginOperateDate",beginOperateDate);
        quereParams.put("endOperateDate",endOperateDate);
        if(StringUtils.isNotEmpty(userRoles)){
            quereParams.put("userRoles",Convert.toStrArray(userRoles));
        }
        if(StringUtils.isEmpty(orgCode)){
            orgCode = ShiroUtils.getSysUser().getDeptId();
        }
        quereParams.put("deptId",orgCode);
        StatisticsReportBusiness totalAll = new StatisticsReportBusiness("","","","",
                "合计",":");
        if (statType == 1) {//按子机构统计
            List<StatisticsReportBusiness> datas = paeaReportBusinessMapper.statisticsByOrgan(quereParams);
            if(datas.size()>0){
                StatisticsReportBusiness total = null;
                String preParentOrgCode = "";
                for(int i=0;i<datas.size();i++) {
                    StatisticsReportBusiness data = datas.get(i);
                    String parentOrgCode = data.getParentOrgCode();
                    if(i==0){
                        preParentOrgCode = parentOrgCode;
                    }else{
                        preParentOrgCode = datas.get(i-1).getParentOrgCode();
                    }
                    if(i==0){
                        total = new StatisticsReportBusiness(data.getParentOrgCode(),data.getParentOrgName(),"","",
                                "小计",":");
                    }
                    if(!parentOrgCode.equals(preParentOrgCode)){
                        result.add(total);
                        totalAll.addReportBusinessNum(total);
                        total = new StatisticsReportBusiness(data.getParentOrgCode(),data.getParentOrgName(),"","",
                                "小计",":");
                        result.add(data);//将数据插入最终表
                        total.addReportBusinessNum(data);//子机构小计
                    }else{
                        result.add(data);//将数据插入最终表
                        total.addReportBusinessNum(data);//子机构小计
                    }
                    if(i==datas.size()-1){
                        result.add(total);
                        totalAll.addReportBusinessNum(total);
                    }
                }
            }
            quereParams.put("isBelowDept",false);
            datas = paeaReportBusinessMapper.statisticsByOrganTeller(quereParams);
            Dept dept = deptMapper.selectDeptById(orgCode);
            if(datas.size()>0) {
                StatisticsReportBusiness total = new StatisticsReportBusiness(dept.getDeptId(),dept.getDeptName(),"","",
                        "小计",":");
                for (StatisticsReportBusiness data : datas) {
                    data.setParentOrgCode(dept.getDeptId());
                    data.setParentOrgName(dept.getDeptName());
                    total.addReportBusinessNum(data);
                }
                result.addAll(datas);
                result.add(total);
                totalAll.addReportBusinessNum(total);
            }
        }else if (statType == 2){//按机构操作员统计
            quereParams.put("isBelowDept",true);
            List<StatisticsReportBusiness> datas = paeaReportBusinessMapper.statisticsByOrganTeller(quereParams);
            Dept dept = deptMapper.selectDeptById(orgCode);
            if(datas.size()>0) {
                for (StatisticsReportBusiness data : datas) {
                    data.setParentOrgCode(dept.getDeptId());
                    data.setParentOrgName(dept.getDeptName());
                    totalAll.addReportBusinessNum(data);
                }
                result.addAll(datas);
            }
        }
        if(result.size()>0){result.add(totalAll);}
        return result;
    }
    public List<Dept> findSonDepts(String orgCode) {
        List<Dept> depts = deptMapper.selectChildrenDeptByParentId(orgCode);
        return depts;
    }
}
